package com;

import method.JdbcDemo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int u_id= Integer.parseInt(request.getParameter("userid"));
        String password=request.getParameter("password");
        String vcode=request.getParameter("vcode");
        String role= request.getParameter("role");



        String flag = (String)request.getSession().getAttribute("flag");


        System.out.println("账号:"+u_id+"  密码:"+password+"  验证码:"+vcode+"  角色:"+role);

        HttpSession session=request.getSession();
        String v_code= (String) session.getAttribute("code");

        JdbcDemo message=new JdbcDemo();
        String result=message.checkUser(u_id,password,role);

        if(vcode.equalsIgnoreCase(v_code)&&result.equals("loginsuccess")){
           // System.out.println("登陆成功！！");
            request.getSession().setAttribute("flag","login_success");
            request.getSession().setAttribute("role",role);
            request.getSession().setAttribute("u_id",u_id);

            request.getRequestDispatcher("/MenuServlet").forward(request,response);

        }else{
          //  System.out.println("登录失败");
            request.getSession().setAttribute("flag","login_fail");
            request.getSession().setAttribute("msg","登录失败，请重新登录");
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }

        /*if (vcode.equalsIgnoreCase(v_code)){
            System.out.println("验证码正确！！");
        }else {
            System.out.println("验证码错误！");
        }


        if (result.equals("hasUserNameAndPasswordCorrect")){

        }*/

    }
}
